package com.hh.spare.service.impl;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/7/28
 * @VERSON:1.8
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.commen.PageVO;
import com.hh.spare.bean.Spare;
import com.hh.spare.bean.SpareLegalPerson;
import com.hh.spare.mapper.SpareLegalPersonMapper;
import com.hh.spare.mapper.SpareMapper;
import com.hh.spare.service.SpareService;
import com.hh.spare.vo.AddSpareVo;
import com.hh.spare.vo.SpareVo;
import com.hh.spare.vo.StatusChangeVo;
import com.hh.spare.vo.UpdateSpareVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SpareServiceImpl implements SpareService {
    @Autowired
    private SpareMapper spareMapper;

    @Autowired
    private SpareLegalPersonMapper spareLegalPersonMapper;

    public PageVO findAllByPage(SpareVo spareVo) {
        PageHelper.startPage((spareVo.getPageIndex()), spareVo.getPageSize());
        List<Spare> spareList = spareMapper.selectAllSpareByPage(spareVo);
        PageInfo<Spare> pageInfo = new PageInfo<>(spareList);
        PageVO pageVO = new PageVO();
        pageVO.setData(spareList);
        pageVO.setTotalNumber(pageInfo.getTotal());
        pageVO.setTotalPage(pageInfo.getPageNum());
        return pageVO;
    }

    @Override
    public ResultVo updateStatus(StatusChangeVo statusChangeVo) {
        return ResultVo.success(spareMapper.updateStatusById(statusChangeVo));
    }

    @Override
    public ResultVo GetLegalPersonId(Integer id) {
        return ResultVo.success(spareMapper.selectAllById(id));
    }

    @Override
    @Transactional
    public ResultVo addSpare(AddSpareVo addSpareVo) {
        if (spareMapper.judgeAddSpareCodeIsExist(addSpareVo.getCode()) > 0) {
            return ResultVo.error("备件编码重复，请重新输入");
        }
        if (spareMapper.judgeAddSpareNameIsExist(addSpareVo.getName()) > 0) {
            return ResultVo.error("备件名称重复，请重新输入");
        }
        if (spareMapper.judgePlanCategoryStatus(addSpareVo.getPlanClassId()) == 0) {
            return ResultVo.error("计划大类状态为禁用，请重新选择");
        }
        if (spareMapper.judgeSubClassStatus(addSpareVo.getSubClassId()) == 0) {
            return ResultVo.error("备件小类状态为禁用，请重新选择");
        }
        if (addSpareVo.getLegalPersonIds().length == 0) {
            return ResultVo.error("法人不能为空");
        }
        for (int i = 0; i < addSpareVo.getLegalPersonIds().length; i++) {
            if (spareMapper.judgeLegalPersonStatus(addSpareVo.getLegalPersonIds()[i]) == 0) {
                return ResultVo.error("选择的法人状态为禁用，请重新选择");
            }
        }
        Spare spare = new Spare();
        spare.setSpareCode(addSpareVo.getCode());
        spare.setSpareName(addSpareVo.getName());
        if (addSpareVo.getStatus()) {
            spare.setStatus(1);
        } else {
            spare.setStatus(0);
        }
        spare.setSpareChildId(addSpareVo.getSubClassId());
        spare.setPlanCategoryId(addSpareVo.getPlanClassId());
        spare.setLegalPersonIds(addSpareVo.getLegalPersonIds());
        Date date = new Date();
        spare.setCreateTime(date);
        spare.setUpdateTime(date);
        try {
            int rowInsertSpare = spareMapper.insert(spare);
            Integer spareId = spareMapper.selectSpareId(addSpareVo.getCode());
            List<SpareLegalPerson> spareLegalPersonList = new ArrayList<>();
            for (int i = 0; i < addSpareVo.getLegalPersonIds().length; i++) {
                SpareLegalPerson spareLegalPerson = new SpareLegalPerson();
                spareLegalPerson.setSpareId(spareId);
                spareLegalPerson.setLegalPersonId(addSpareVo.getLegalPersonIds()[i]);
                spareLegalPersonList.add(spareLegalPerson);
            }
            int rowInsertSpareLegalPerson = spareLegalPersonMapper.insertSpareLegalPerson(spareLegalPersonList);
            if (rowInsertSpareLegalPerson > 0 && rowInsertSpare > 0) {
                return ResultVo.success();
            }
        } catch (RuntimeException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return ResultVo.error("添加失败");
    }

    @Override
    @Transactional
    public ResultVo updateSpare(UpdateSpareVo updateSpareVo) {
        if (spareMapper.judgeUpdateSpareCodeIsExist(updateSpareVo.getId(), updateSpareVo.getCode()) > 0) {
            return ResultVo.error("备件编码不能重复");
        }
        if (spareMapper.judgeUpdateSpareNameIsExist(updateSpareVo.getId(), updateSpareVo.getName()) > 0) {
            return ResultVo.error("备件名称不能重复");
        }
        if (spareMapper.judgePlanCategoryStatus(updateSpareVo.getPlanClassId()) == 0) {
            return ResultVo.error("计划大类状态为禁用，请重新选择");
        }
        if (spareMapper.judgeSubClassStatus(updateSpareVo.getSubClassId()) == 0) {
            return ResultVo.error("备件小类状态为禁用，请重新选择");
        }
        if (updateSpareVo.getLegalPersonId().length == 0) {
            return ResultVo.error("法人不能为空");
        }
        for (int i = 0; i < updateSpareVo.getLegalPersonId().length; i++) {
            if (spareMapper.judgeLegalPersonStatus(updateSpareVo.getLegalPersonId()[i]) == 0) {
                return ResultVo.error("选择的法人状态为禁用，请重新选择");
            }
        }
        Spare spare = new Spare();
        spare.setId(updateSpareVo.getId());
        spare.setSpareCode(updateSpareVo.getCode());
        spare.setSpareName(updateSpareVo.getName());
        spare.setStatus(updateSpareVo.getStatus());
        spare.setSpareChildId(updateSpareVo.getSubClassId());
        spare.setPlanCategoryId(updateSpareVo.getPlanClassId());
        Date date = new Date();
        spare.setUpdateTime(date);
        spare.setVersion(updateSpareVo.getVersion());
        spare.setLegalPersonIds(updateSpareVo.getLegalPersonId());
        try {
            int rowUpdateSpare = spareMapper.updateByPrimaryKeySelective(spare);
            int rowDelete = spareLegalPersonMapper.deleteLegalPersonIdById(updateSpareVo.getId());
            List<SpareLegalPerson> spareLegalPersonList = new ArrayList<>();
            for (int i = 0; i < updateSpareVo.getLegalPersonId().length; i++) {
                SpareLegalPerson spareLegalPerson = new SpareLegalPerson();
                spareLegalPerson.setSpareId(updateSpareVo.getId());
                spareLegalPerson.setLegalPersonId(updateSpareVo.getLegalPersonId()[i]);
                spareLegalPersonList.add(spareLegalPerson);
            }
            int insertLegalPersonId = spareLegalPersonMapper.insertSpareLegalPerson(spareLegalPersonList);
            if (rowUpdateSpare > 0 && rowDelete > 0 && insertLegalPersonId > 0) {
                return ResultVo.success();
            }
        } catch (RuntimeException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return ResultVo.error("更新失败");
    }


}
